<template>
  <Container class="member-detail">
    <div class="container-header title title-center" slot="header">
      {{id ? '编辑' : '新增'}}品牌例外政策
      <Button size="large" class="fl btn-back" type="ghost" icon="ios-arrow-back" @click="jumpPrevPage">返回</Button>
    </div>
    <div class="bg-light mt pt">
      <Form :model="form" :label-width="100" ref="form" :rules="rules">
         <FormItem :label-width='20'> 
              <div class="common-class">
                <i class="ivu-icon ivu-icon-ios-alert"></i>
                <span> 提示：如某轮胎符合政策里的参数(为空的参数忽略)，则这个轮胎的条码可以忽略品牌进行特价订单匹配/积分返利赠送优惠券激活/月度季度任务/基金/月度季度积分返利等事项</span>
                </div>
          </FormItem>
        <FormItem label="政策名称" class="wp40" prop="policyName">
          <Input 
            class="start"
            v-model.trim="form.policyName"
            placeholder="政策名称">
          </Input>
        </FormItem>
        <FormItem label="尺寸" class="wp40" >
          <Select v-model="form.tiresRim" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_sizeArr' :value='item.value' :key="item.value">{{item.text}}</Option>
          </Select>
        </FormItem>
        <FormItem label="胎面宽" class="wp40">
          <Select v-model="form.tiresWidth" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_widthArr' :value='item.value' :key="item.value">{{item.text}}</Option>
          </Select>
        </FormItem>
        <FormItem label="扁平率" class="wp40">
          <Select v-model="form.tiresAspectratio" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_ratioArr' :value='item.value' :key="item.value">{{item.text}}</Option>
          </Select>
        </FormItem>
        <FormItem label="花纹" class="wp40">
          <Select v-model="form.tiresFigure" clearable filterable placeholder='全部'>
            <Option v-for='item in tire_figure' :value='item.dataValue' :key="item.id">{{item.dataValue}}</Option>
          </Select>
        </FormItem>
        <FormItem label="类型标识" class="wp40">
          <Select v-model="form.tiresType" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_type_symbol' :value='item.dataValue' :key="item.id">{{item.dataValue}}</Option>
          </Select>
        </FormItem>
        <FormItem label="层级" class="wp40">
          <Select v-model="form.tiresLayer" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_layer' :value='item.id' :key="item.id">{{item.dataValue}}</Option>
          </Select>
        </FormItem>
        <FormItem label="速度级别" class="wp40">
          <Select v-model="form.tiresSpeed" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_speed_level' :value='item.id' :key="item.id">{{item.dataValue}}</Option>
          </Select>
        </FormItem>
        <FormItem label="负荷指数" class="wp40">
          <Select v-model="form.loadIndex" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_load_index' :value='item.id' :key="item.id">{{item.dataValue}}</Option>
          </Select>
        </FormItem>
      </Form>
    </div>
    <Loader :status="status"></Loader>
    <div class="container-footer bg-white" slot="footer">
      <Button size="large" class="fr btn-back" type="primary" @click="onSave" :disabled="disabled">确定</Button>
    </div>
  </Container>    
</template>

<script>
import BrandSelect from '@/components/BrandSelect'
import { formatDate, getTireArr, comparativeDate } from '@/util'
export default {
  components: {
    BrandSelect
  },
  data() {
    return {
      id: this.$route.query.id || '',
      tire_widthArr: [], //胎面宽
      tire_ratioArr: [], //扁平率
      tire_sizeArr: [], //尺寸
      tire_figure: [], //花纹
      tire_layer: [], //层级
      tire_speed_level: [], //速度级别
      tire_load_index: [], //负荷指数
      tire_type_symbol: [], //类型标识
      status: 'pass',
      title: '新增返利例外政策',
      disabled: false,
      rules: {
        policyName: [{ required: true, message: '政策名称不能为空', trigger: 'blur' }]
      },
      form: {
        id: '',
        policyName: null,
        tiresWidth: null,
        tiresRim: null,
        tiresAspectratio: null,
        tiresFigure: null,
        tiresSpeed: null,
        tiresType: null,
        tiresLayer: null,
        loadIndex: null,
      }
    }
  },
  mounted() {
    if (this.id) this.getInfo()
    this.getTireData()
    this.getTireFigure()
    this.getTireLayer()
    this.getTireSpeedLevel()
    this.getTireLoadIndex()
    this.getTireTypeSymbol()
  },
  methods: {
    getTireData() {
      getTireArr().then(data => {
        this.tire_ratioArr = data.data.aspect_ratio.filter(item => item.val).map(item => { //扁平率
          return {
            value: item.val,
            text: item.val ? item.name : ''
          }
        })
        this.tire_sizeArr = data.data.tire_rim.filter(item => item.val).map(item => { //尺寸
          return {
            value: item.val,
            text: item.name
          }
        })
        this.tire_widthArr = data.data.width.filter(item => item.val).map(item => { //胎面宽
          return {
            value: item.val,
            text: item.name
          }
        })
      })
    },
    //花纹
    getTireFigure() {
      this.$ajaxGet('/product/common/list/TIRE_FIGURE').then(data =>{
        this.tire_figure = (data || []).map(item =>{
          return {
            dataValue: item.dataValue,
            id: item.id
          }
        })
      })
    },
    //层级
    getTireLayer() {
      this.$ajaxGet('/product/common/list/TIRE_LAYER').then(data =>{
        this.tire_layer = (data || []).map(item =>{
          return {
            dataValue: item.dataValue,
            id: item.id
          }
        })
      })
    },
    //速度级别
    getTireSpeedLevel() {
      this.$ajaxGet('/product/common/list/TIRE_SPEED_LEVEL').then(data =>{
        this.tire_speed_level = (data || []).map(item =>{
          return {
            dataValue: item.dataValue,
            id: item.id
          }
        })
      })
    },
    //负荷指数
    getTireLoadIndex() {
      this.$ajaxGet('/product/common/list/TIRE_LOAD_INDEX').then(data =>{
        this.tire_load_index = (data || []).map(item =>{
          return {
            dataValue: item.dataValue,
            id: item.id
          }
        })
      })
    },
    //类型标识
    getTireTypeSymbol() {
      this.$ajaxGet('/product/common/list/TIRE_TYPE_SYMBOL').then(data =>{
        this.tire_type_symbol = (data || []).map(item =>{
          return {
            dataValue: item.dataValue,
            id: item.id
          }
        })
      })
    },
    getInfo() {
      this.$ajaxGet('/setting/brand_special_policy/detail/' + this.id).then(data => {
        this.form = {
          id: data.id,
          policyName: data.policyName,
          tiresAspectratio: data.tiresAspectratio,
          tiresFigure: data.tiresFigure,
          tiresRim: data.tiresRim,
          tiresSpeed: data.tiresSpeed,
          tiresWidth: data.tiresWidth,
          tiresType: data.tiresType,
          loadIndex: data.loadIndex,
          tiresLayer: data.tiresLayer
        }
      })
    },
    onSave() {
      if (this.disabled) return
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.tiresAspectratio || this.form.tiresFigure || this.form.tiresRim || this.form.tiresSpeed || this.form.tiresWidth || this.form.tiresType || this.form.loadIndex || this.form.tiresLayer) {
            let text = this.id ? '编辑' : '新增'
            let url = this.id ? '/setting/brand_special_policy/modify' : '/setting/brand_special_policy/save'
            let param = {
              id: this.form.id,
              policyName: this.form.policyName,
              tiresAspectratio: this.form.tiresAspectratio,
              tiresFigure: this.form.tiresFigure,
              tiresRim: this.form.tiresRim,
              tiresSpeed: this.form.tiresSpeed,
              tiresWidth: this.form.tiresWidth,
              tiresType: this.form.tiresType,
              loadIndex: this.form.loadIndex,
              tiresLayer: this.form.tiresLayer
            }
            if (this.id) param.id = this.id
            this.status = 'loading'
            this.$jsonPost(url, param).then(data => {
              this.$Notice.success({
                title: `${text}成功`
              })
              this.status = 'pass'
              this.disabled = false
              this.$router.push('/policy/brandException/list')
              
            }).catch(() => {
              this.status = 'pass'
              this.disabled = false
            })
          } else {
            this.$Notice.warning({
              title: '除政策名外，必须再填一项或一项以上'
            })
          }
        } else {
          this.disabled = false
        }
      })
    },
    jumpPrevPage() {
      this.$router.back()
    }
  }
}
</script>